<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>文章管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="文章名称">
					<el-input v-model="queryInfo.articleName" @clear="queryList" placeholder="请输
入文章名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddArticle">新增文章</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="articleList" border style="width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="articleId" label="ID"></el-table-column>
			<el-table-column prop="articleName" label="文章名称">
			</el-table-column>
			<el-table-column prop="articleIntro" label="文章简介">
			</el-table-column>
			<el-table-column prop="consult.consultName" label="文章作者">
			</el-table-column>
			<el-table-column prop="articlePicture" label="文章图片">
				<template slot-scope='scope'>
					<el-image style="width: 100px;
					height: 100px" :src="url+'download?filename='+scope.row.articlePicture"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="articleType.articleTypeName" label="文章类型">
			</el-table-column>
			<el-table-column prop="articleRead" label="阅读数量">
			</el-table-column>
			<el-table-column prop="articleLike" label="收藏数量">
			</el-table-column>
			<el-table-column prop="articleCreatTime" label="创建时间">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditArticle(scope.row)">编辑文章</el-link>
					<el-link type="primary" @click="toDeleteArticle(scope.row.articleId)">删除文章</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>


		<!-- 新增和修改的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="article">
				<el-form-item label="文章名称" label-width="80px">
					<el-input placeholder="请输入文章名称" v-model="article.articleName"></el-input>
				</el-form-item>
				<!-- <el-form-item label="作者" label-width="80px">
					<el-input placeholder="请输入作者" v-model="article.consultId"></el-input>
				</el-form-item> -->
				<el-form-item label="作者" label-width="80px">
					<el-select v-model="article.consultId" filterable placeholder="请选择">
					    <el-option
					      v-for="item in consultList"
					      :key="item.consultId"
					      :label="item.consultName"
					      :value="item.consultId">
					    </el-option>
					  </el-select>
				</el-form-item>
				<el-form-item label="文章简介" label-width="80px">
					<el-input placeholder="请输入文章简介" v-model="article.articleIntro"></el-input>
				</el-form-item>

				<el-form-item label="文章图片" label-width="80px">
					<!-- <el-input placeholder="请输入文章图片" v-model="article.articlePicture"></el-input> -->
					<!-- 
								 el-upload:上传组件
									action： 上传网址
									list-type：上传组件类型
									on-preview ： 图片预览事件
									on-remove ：移除图片事件
									el-dialog : 预览图片组件
									on-success : 上传成功后的回调函数
									limit  ： 限制上传图片个数
									fileList:就是用来展示图片的路径的数组
							-->
					<el-upload :action="url+'upload'" list-type="picture-card" :on-success="handleSuccess" :limit="1"
						:on-preview="handlePictureCardPreview" :on-remove="handleRemove" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>

				<el-form-item label="文章类型" label-width="80px">
					<el-select v-model="article.articleTypeId" filterable placeholder="请选择">
					    <el-option
					      v-for="item in articleTypeList"
					      :key="item.articleTypeId"
					      :label="item.articleTypeName"
					      :value="item.articleTypeId">
					    </el-option>
					  </el-select>
				</el-form-item>

				<el-form-item label="文章详情" label-width="80px">
					<!-- <el-input placeholder="请输入文章详情" v-model="article.articleDetails">	</el-input> -->
					<!-- 富文本编辑器组件 -->
					<quill-editor v-model="article.articleDetails"></quill-editor>

				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addArticle">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		url
	} from '../../util/tool.js'
	export default {
		data() {
			return {

				url: '', // 定义一个全局的url
				// 查询信息对象
				queryInfo: {
					articleName: '',
					pageNum: 1,
					pageSize: 10
				},
				total: 0,
				articleList: [],
				title: '',
				dialogFormVisible: false,
				article: { // 文章对象
					"articleId": '',
					"articleName": "",
					"articleIntro": "",
					"articlePicture": "",
					"articleDetails": "",
					"articleRead": 0,
					"articleLike": 0,
					"consultId": 1,
					"articleTypeId": 1,
					"articleCreateTime": ""
				},
				dialogImageUrl: '', // 预览图片路径
				dialogVisible: false, // 预览图片的对话框
				fileList: [],
				articleTypeList:[],
				consultList:[]
			}
		},
		methods: {
			queryconsultList() {
			  //调用后端接口获取数据
			  this.$http
			    .get("consult/findAllConsult")
			    .then((res) => {
			      if (res.data.status == 200) {
			       
			        this.consultList = res.data.data;
					console.log(this.consultList);
			      } else {
			        this.$message.error("查询失败！");
			      }
			    });
			},
			// 分页查询所有的数据
			queryarticleTypeList() {
				//	调用后端接口
				this.$http.get('articleType')
					.then(res => {
						if (res.data.status == 200) {
							this.articleTypeList = res.data.data
						} else {
							this.$message.error("服务器异常！")
						}
					})
			},
			// 处理上传成功的函数
			handleSuccess(response) {
				// 把上传后的图片路径，交给文章对象的articlePicture
				this.article.articlePicture = response.message;
			},

			// 删除图片的事件
			handleRemove(file, fileList) {
				console.log(file, fileList);
				// 移除数据
				this.article.articlePicture = '';
			},

			// 预览图片事件
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			cancle() {
				this.article = { // 文章对象
					"articleId": '',
					"articleName": "",
					"articleIntro": "",
					"articlePicture": "",
					"articleDetails": "",
					"articleRead": 0,
					"articleLike": 0,
					"consultId": 0,
					"articleTypeId": 0,
					"articleCreatTime": ""
				}
				// 关闭对话框
				this.dialogFormVisible = false;
				// 重新查询
				this.queryList()
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},
			//	去删除文章内容
			toDeleteArticle(articleId) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('article/' + articleId)
							.then(res => {
								if (res.data.status == 200) {
									this.$message.success(res.data.message)
									this.queryList()
								} else {
									this.$message.error(res.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},

			//	去修改文章内容
			toEditArticle(row) {
				console.log(row.articlePicture);
				// 赋值
				this.article = row;
				// 设置标题
				this.title = "修改文章";
				// 开启对话框
				this.dialogFormVisible = true;
				// 动态的获取图片的名称并帮到到fileList
				this.fileList = [{
					name: row.articlePicture,
					url: this.url + 'download?filename=' + row.articlePicture
				}];
			},

			//	添加或修改文章内容
			addArticle() {
				if (this.article.articleId) { //	修改	true
					this.$http.put("article", this.article)
						.then(res => {
							if (res.data.status == 200) {
								this.$message.success("修改成功！")
								// 清空数据
								this.article = { // 文章对象
									"articleId": '',
									"articleName": "",
									"articleIntro": "",
									"articlePicture": "",
									"articleDetails": "",
									"articleRead": 0,
									"articleLike": 0,
									"consultId": 0,
									"articleTypeId": 0,
									"articleCreatTime": ""
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error("修改失败！")
							}
						})
				} else { //	添加
					this.$http.post("article", this.article)
						.then(res => {
							if (res.data.status == 200) {
								this.$message.success("添加成功！")
								// 清空数据
								this.article = { // 文章对象
									"articleId": '',
									"articleName": "",
									"articleIntro": "",
									"articlePicture": "",
									"articleDetails": "",
									"articleRead": 0,
									"articleLike": 0,
									"consultId": 1,
									"articleTypeId": 1,
									"articleCreatTime": ""
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error("添加失败！")
							}
						})
				}
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},

			// 进入添加的对话框
			toAddArticle() {
				this.title = "新增文章";
				this.dialogFormVisible = true;

			},

			// 分页查询所有的数据
			queryList() {
				//	调用后端接口
				this.$http.get('article/findByPage', {
						params: this.queryInfo
					})
					.then(res => {
						if (res.data.status == 200) {
							
							this.articleList = res.data.data.list
							this.total = res.data.data.total
						} else {
							this.$message.error("服务器异常！")
						}
					})
			},
			handleSizeChange(size) {
				// console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size
				this.queryInfo.pageNum = 1
				this.queryList()
			},
			handleCurrentChange(num) {
				// console.log('页码发生改变');
				this.queryInfo.pageNum = num
				this.queryList()
			}
		},
		mounted() {
			this.queryList()
			// 动态的获取tool.js中的url，并赋值给当前组件的全局url
			this.url = url;
			this.queryarticleTypeList()
			this.queryconsultList()
		}
	}
</script>
<style>
	/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
	.ql-container {
		min-height: 200px;
	}

	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>